package com.fwtai.builder;

//传统算法
public final class IdWorkerBase extends IdWorkerOptimize{

  public IdWorkerBase(final IdWorkerOptions options) {
    super(options);
  }

  @Override
  public long nextId(){
    synchronized (_SyncLock) {
      long currentTimeTick = GetCurrentTimeTick();
      if (_LastTimeTick == currentTimeTick) {
        if (_CurrentSeqNumber++ > MaxSeqNumber) {
          _CurrentSeqNumber = MinSeqNumber;
          currentTimeTick = GetNextTimeTick();
        }
      } else {
        _CurrentSeqNumber = MinSeqNumber;
      }
      if (currentTimeTick < _LastTimeTick) {
        throw new IdWorkerException("Time error for {0} milliseconds", _LastTimeTick - currentTimeTick);
      }
      _LastTimeTick = currentTimeTick;
      return ((currentTimeTick << _TimestampShift) + ((long) WorkerId << SeqBitLength) + (int) _CurrentSeqNumber);
    }
  }
}